package com.ysd.lis.service.bac.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.BaseEntity;
import com.ysd.lis.entity.SysUser;
import com.ysd.lis.entity.bac.BacAntProposalGroupDict;
import com.ysd.lis.entity.sta.StaAntProposalGroup;
import com.ysd.lis.mapper.bac.BacAntProposalGroupDictMapper;
import com.ysd.lis.request.bac.BacGermBaseDictRequest;
import com.ysd.lis.service.bac.BacAntProposalGroupDictService;
import com.ysd.util.RedisUserManager;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;


/**
 * @author LuoXS
 * @date 2024/3/28
 * version 1.0
 */
@Service
public class BacAntProposalGroupDictServiceImpl extends ServiceImpl<BacAntProposalGroupDictMapper, BacAntProposalGroupDict> implements BacAntProposalGroupDictService {

    @Autowired
    BacAntProposalGroupDictMapper mapper;


    @Override
    public Result add(BacAntProposalGroupDict dict) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                dict.setCreator(sysUser.getCreator());
                dict.setOrgId(sysUser.getOrgId());
            }
            boolean save = this.save(dict);
            if (!save) {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("添加失败！");
        }
        return result;
    }

    @Override
    public Result updateDict(BacAntProposalGroupDict dict) {
        boolean b = this.updateById(dict);
        if (b) {
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result pageList(BacGermBaseDictRequest.BacAntProposalGroupDictPageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<BacAntProposalGroupDict> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            Page<BacAntProposalGroupDict> page = new Page<>();
            page.setCurrent(pageParam.getPageIndex());
            page.setSize(pageParam.getPageSize());
            // 参数
            if (!StringUtils.isEmpty(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(wrapper ->
                        wrapper.like(BacAntProposalGroupDict::getName, pageParam.getSearchValue())
                                .or().like(BacAntProposalGroupDict::getStandardName, pageParam.getSearchValue()));

            }
            lambdaQueryWrapper.eq(BacAntProposalGroupDict::getDelFlag, 0);
            lambdaQueryWrapper.orderByAsc(BacAntProposalGroupDict::getDisplayOrder);
            Page<BacAntProposalGroupDict> selectPage = mapper.selectPage(page, lambdaQueryWrapper);
            result = Result.succ(selectPage);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result delete(BacGermBaseDictRequest.BacAntProposalGroupDictDeleteParam deleteParam) {
        Result result;
        try {
            if (ToolsUtils.isEmpty(deleteParam) || ToolsUtils.isEmpty(deleteParam.getIds())) {
                return Result.fail("入参不可为空！");
            }
            String[] array = deleteParam.getIds().split(",");
            boolean b = this.removeByIds(Arrays.asList(array));
            if (b) {
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

    @Override
    public Result findDictList(BacAntProposalGroupDict param) {
        Result result;
        try {
            LambdaQueryWrapper<BacAntProposalGroupDict> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(BaseEntity::getDelFlag, false)
                    .isNotNull(BacAntProposalGroupDict::getName)
                    .isNotNull(BacAntProposalGroupDict::getCode);

            SysUser sysUser = RedisUserManager.getUser();
            if (ToolsUtils.isNotEmpty(sysUser)) {
                wrapper.eq(BacAntProposalGroupDict::getOrgId, sysUser.getOrgId());
            }

            List<BacAntProposalGroupDict> dicts = mapper.selectList(wrapper);
            result = Result.succ(dicts);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result staDataSyncBacAntGroup(List<StaAntProposalGroup> staAntProposalGroups) {
        for (StaAntProposalGroup staAntProposalGroup : staAntProposalGroups) {
            BacAntProposalGroupDict bacAntProposalGroupDict = new BacAntProposalGroupDict();
            BeanUtils.copyProperties(staAntProposalGroup, bacAntProposalGroupDict, "id");
            this.saveOrUpdate(bacAntProposalGroupDict, new LambdaQueryWrapper<BacAntProposalGroupDict>().eq(BacAntProposalGroupDict::getCode, bacAntProposalGroupDict.getCode()).eq(BacAntProposalGroupDict::getDelFlag, 0));
        }
        return Result.succ(1,"同步成功！",null);
    }
}
